<template>
  <el-card class="attr-card">
    <!-- 要阻止默认提交,不然按回车就会提交表单 -->
    <el-form @submit.prevent>
      <el-form-item label="属性名">
        <el-input placeholder="请输入属性名" v-model="attrFormDate.attrName" class="attr-input" align="center" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Plus" :disabled="!attrFormDate.attrName" @click="addAttrValueList">添加属性值</el-button>
      </el-form-item>
      <el-form-item>
        <el-table :data="attrFormDate.attrValueList" style="width: 100%">
          <el-table-column type="index" label="序号" width="80" />
          <el-table-column label="属性值名称">
            <!-- 因为会渲染两个标签,所以需要条件渲染 -->
            <template v-slot="{ row, $index }">
              <el-input
                v-if="row.isEdit"
                size="small"
                ref="inputRef"
                v-model="row.valueName"
                @blur="ShowValueName(row, $index)"
                @keyup.enter="ShowValueName(row, $index)"
              />
              <span v-else class="attr-value" @click="ShowInput(row)">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="{ row, $index }">
              <el-popconfirm :title="`确定删除${row.valueName}吗?`" @confirm="deleteAttrValueList($index)">
                <template #reference>
                  <el-button type="danger" size="small" :icon="Delete"></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :disabled="!isShowTrue" @click="submitFrom">确定</el-button>
        <el-button @click="$emit('update:modelValue', !modelValue)">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts">
export default {
  name: "AddSetAttr",
};
</script>

<script lang="ts" setup>
import { reactive, ref, nextTick, computed } from "vue";
import { Plus, Delete } from "@element-plus/icons-vue";
import { addAttrApi } from "@/api/product/attr";
import { useCategoryStore } from "@/stores/category";
import type { AttrItem,AttrValueItem } from "@/api/product/models/attrModel";
const CategoryStore = useCategoryStore();
import { ElMessage } from "element-plus";

//接收props数据
const props = defineProps<{
  modelValue: boolean;
  attr: AttrItem;
}>();
//接收自定义事件
const emits = defineEmits(["update:modelValue"]);

//定义表单表格数据
const attrFormDate = reactive<AttrItem>(props.attr);

// 获取输入框的DOM元素
const inputRef = ref();

//添加属性值的回调
const addAttrValueList = async () => {
  //往数组中添加一个{},就多显示一行数据
  //每次添加一个标识,从而去判断显示input还是文本
  attrFormDate.attrValueList.push({
    valueName: "", //属性值名称
    isEdit: true, //标识
  });
  //此时数据更新是同步的,页面渲染时异步的
  // 等待DOM元素更新完
  await nextTick();
  // 让输入框聚焦
  inputRef.value.focus();
};

//失去焦点/回车 的回调
const ShowValueName = (row: AttrValueItem, index: number) => {
  //判断当输入空值的时候,失去焦点时把这条数据删除
  if (!row.valueName) {
    //通过下标来操作
    attrFormDate.attrValueList.splice(index, 1);
    return;
  }
  row.isEdit = false;
};

//点击属性值名称的回调
const ShowInput = async (row: AttrValueItem) => {
  row.isEdit = true;
  // 等待DOM元素更新完
  await nextTick();
  // 让输入框聚焦
  inputRef.value.focus();
};

//删除属性值的回调
const deleteAttrValueList = (index: number) => {
  attrFormDate.attrValueList.splice(index, 1);
};

//确定按钮禁用效果
//判断是否有属性名和属性值,有的话显示
const isShowTrue = computed(() => {
  return (
    attrFormDate.attrName &&
    attrFormDate.attrValueList.find((item) => {
      return item.valueName;
    })
  );
});

//提交表单(添加/修改属性的回调)
const submitFrom = async () => {
  await addAttrApi({
    ...attrFormDate,
    categoryId: CategoryStore.category3Id as number,
    categoryLevel: 3,
  });
  ElMessage.success(`${attrFormDate.id ? "修改" : "添加"}属性成功`);
  //重新回到属性列表界面
  emits("update:modelValue", !props.modelValue);
};

</script>

<style scoped>
.attr-card {
  margin-top: 20px;
}
.attr-input {
  width: 200px;
  margin-left: 10px;
}
.attr-value {
  display: block;
  width: 100%;
}
</style>
